/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Class to Print the Data (Report-Generation Main class)
*  Name: PrintData.java
*  Author: www.pecats.com
*  Date: Sept 15, 2008
************************************************************/

package com.pecats.batch.core;

import com.pecats.batch.data.HitsData;
import com.pecats.batch.data.ConfigData;
import com.pecats.batch.util.ParseXMLFile;
import com.pecats.batch.main.openXplore;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.FileWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 * Used to print results after reading a log file
 */

public class PrintData
{

	HitsData hd;
	int urlCounter=0;
	int yearCounter=0;
	int monthCounter=0;
	int dayCounter=0;
	int hourCounter=0;
	int hour=0;
	int statuscodeCounter=0;
	int browserCounter=0;


	int dayData = 0;			/* Flag to ascertain whether data is present in HitsData */

	// Variables to write to an XML file
	File aFile = null;
	BufferedWriter output = null;
	FileWriter fWriter = null;
	
	// Variables to write to main HTML file
	File mainFile = null;
	BufferedWriter mainOutput = null;
	FileWriter mainWriter = null;

	public static String apacheextended = "N";

	public PrintData(HitsData hitsdata)
	{
		hd = hitsdata;
	}

	// Print a simple Report from the data
	public void printXML(File file, File aDir)
	{
	
		try
		{
			aFile = file;
			fWriter = new FileWriter(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file.getName()+ ".xml");
			output = new BufferedWriter(fWriter);
	
			// Print URL Data
			output.write("<?xml version=\"1.0\"?>");
			output.newLine();
			output.write("<openXplore>");
			output.newLine();

			// Hits Data Output
			output.write("<HitsData>");
			output.newLine();

			// URLwise Data Output
			output.write("<URLData>");
			output.newLine();


			for (urlCounter=0; urlCounter<hd.getSizeUrlName();urlCounter++)
			{	
				output.write("   ");
				output.write("<URLName id=\"" +hd.getUrlName(urlCounter).toString() + "\">");				
				output.newLine();

				for(dayCounter=0; dayCounter<hd.getSizeDays();dayCounter++)
				{
					for(hourCounter=0;hourCounter<24;hourCounter++)
					{				
						if (hd.getHitsData(hd.getUrlName(urlCounter).toString(), hd.getDays(dayCounter).toString(), hourCounter)!=0)
						{
							dayData = 1;
						}
					}
					if (dayData==1)
					{
						output.write("      ");
						output.write("<Day id=\"" +hd.getDays(dayCounter).toString() + "\">");
						output.newLine();
						//output.write("         ");
						//output.write("<Hours>");
						output.newLine();
						for(hourCounter=0;hourCounter<24;hourCounter++)
						{						
							if (hd.getHitsData(hd.getUrlName(urlCounter).toString(), hd.getDays(dayCounter).toString(), hourCounter)!=0)
							{
								System.out.println(hd.getHitsData(hd.getUrlName(urlCounter).toString(), hd.getDays(dayCounter).toString(), hourCounter));
								output.write("            ");
								output.write("<Hour id=\"" +hourCounter + "\">");
								output.write(Integer.toString(hd.getHitsData(hd.getUrlName(urlCounter).toString(), hd.getDays(dayCounter).toString(), hourCounter)));
								output.write("</Hour>");
								output.newLine();
							}	
						}
						output.write("      "); 
						output.write("</Day>");			
						output.newLine();
					}
					dayData=0;
				}

				output.write("   ");
				output.write("</URLName>");
				output.newLine();
			}


			output.write("</URLData>");

			// HourWise Data Output			
			output.write("<HourWise>");
			output.newLine();
			
			for(hourCounter=0;hourCounter<24;hourCounter++)
			{
				output.write("            ");
				output.write("<Hour id=\"" +hourCounter + "\">");
				for (urlCounter=0; urlCounter<hd.getSizeUrlName();urlCounter++)
				{	
					if (hd.getHourHitsData(hd.getUrlName(urlCounter).toString(), hourCounter)!=0)
					{
						output.write("   ");
						output.write("<URLName id=\"" +hd.getUrlName(urlCounter).toString() + "\">");				
						output.write(Integer.toString(hd.getHourHitsData(hd.getUrlName(urlCounter).toString(), hourCounter)));
						output.write("</URLName>");
						output.newLine();
					}
				}
				output.write("</Hour>");
				output.newLine();							
			}

			output.write("</HourWise>");	
			output.newLine();


			// DayWise Data Output			
			output.write("<DayWise>");
			output.newLine();

			for (dayCounter=0; dayCounter<hd.getSizeDays(); dayCounter++)
			{	
				output.write("   ");
				output.write("<Day id=\"" +hd.getDays(dayCounter).toString() + "\">");				
				output.newLine();
				for(urlCounter=0; urlCounter<hd.getSizeUrlName(); urlCounter++)
				{
					if (hd.getDayHitsData(hd.getUrlName(urlCounter).toString(), hd.getDays(dayCounter).toString())!=0)
					{
						output.write("      ");
						output.write("<URLName id=\"" +hd.getUrlName(urlCounter).toString() + "\">");
						System.out.println(hd.getDayHitsData(hd.getUrlName(urlCounter).toString(), hd.getDays(dayCounter).toString()));
						output.write(Integer.toString(hd.getDayHitsData(hd.getUrlName(urlCounter).toString(), hd.getDays(dayCounter).toString())));
						output.write("</URLName>");
						output.newLine();
					}	
				}
				output.write("   ");
				output.write("</Day>");
				output.newLine();
			}

			output.write("</DayWise>");	
			output.newLine();


			// MonthWise Data Output			
			output.write("<MonthWise>");
			output.newLine();

			for (monthCounter=0; monthCounter<hd.getSizeMonths(); monthCounter++)
			{	
				output.write("   ");
				output.write("<Month id=\"" +hd.getMonths(monthCounter).toString() + "\">");				
				output.newLine();
				for(urlCounter=0; urlCounter<hd.getSizeUrlName(); urlCounter++)
				{
					if (hd.getMonthHitsData(hd.getUrlName(urlCounter).toString(), hd.getMonths(monthCounter).toString())!=0)
					{
						output.write("      ");
						output.write("<URLName id=\"" +hd.getUrlName(urlCounter).toString() + "\">");
						System.out.println(hd.getMonthHitsData(hd.getUrlName(urlCounter).toString(), hd.getMonths(monthCounter).toString()));
						output.write(Integer.toString(hd.getMonthHitsData(hd.getUrlName(urlCounter).toString(), hd.getMonths(monthCounter).toString())));
						output.write("</URLName>");
						output.newLine();
					}	
				}
				output.write("   ");
				output.write("</Month>");
				output.newLine();
			}


			output.write("</MonthWise>");	
			output.newLine();


			// YearWise Data Output			
			output.write("<YearWise>");
			output.newLine();

			for (yearCounter=0; yearCounter<hd.getSizeYears(); yearCounter++)
			{	
				output.write("   ");
				output.write("<Year id=\"" +hd.getYears(yearCounter).toString() + "\">");				
				output.newLine();
				
				for(urlCounter=0; urlCounter<hd.getSizeUrlName(); urlCounter++)
				{
					if (hd.getYearHitsData(hd.getUrlName(urlCounter).toString(), ((Integer)hd.getYears(yearCounter)).intValue())!=0)
					{
						output.write("      ");
						output.write("<URLName id=\"" +hd.getUrlName(urlCounter).toString() + "\">");
						System.out.println(hd.getYearHitsData(hd.getUrlName(urlCounter).toString(), ((Integer)hd.getYears(yearCounter)).intValue()));
						output.write(Integer.toString(hd.getYearHitsData(hd.getUrlName(urlCounter).toString(), ((Integer)hd.getYears(yearCounter)).intValue())));
						output.write("</URLName>");
						output.newLine();
					}	
				}

				output.write("</Year>");
				output.newLine();
			}

			output.write("</YearWise>");	
			output.newLine();


			// StatusCode Data Output
			
			output.write("<StatusCodeData>");
			output.newLine();

			for (statuscodeCounter=0; statuscodeCounter<hd.getSizeStatusCodes(); statuscodeCounter++)
			{	
				output.write("   ");
				output.write("<StatusCode id=\"" +hd.getStatusCodes(statuscodeCounter).toString() + "\">");				
				output.newLine();
				
				for(urlCounter=0; urlCounter<hd.getSizeUrlName(); urlCounter++)
				{
					if (hd.getStatusCodesData(hd.getUrlName(urlCounter).toString(), ((Integer)hd.getStatusCodes(statuscodeCounter)).intValue())!=0)
					{
						output.write("      ");
						output.write("<URLName id=\"" +hd.getUrlName(urlCounter).toString() + "\">");
						System.out.println(hd.getStatusCodesData(hd.getUrlName(urlCounter).toString(), ((Integer)hd.getStatusCodes(statuscodeCounter)).intValue()));
						output.write(Integer.toString(hd.getStatusCodesData(hd.getUrlName(urlCounter).toString(), ((Integer)hd.getStatusCodes(statuscodeCounter)).intValue())));
						output.write("</URLName>");
						output.newLine();
					}	
				}

				output.write("</StatusCode>");
				output.newLine();
			}

			output.write("</StatusCodeData>");	
			output.newLine();

			if (apacheextended == "Y")
			{
				// Browser Data Output
				output.write("<BrowserData>");
				output.newLine();

				for (browserCounter=0; browserCounter<hd.getSizeBrowsers(); browserCounter++)
				{	
					output.write("   ");
					output.write("<Browser id=\"" +hd.getBrowsers(browserCounter).toString() + "\">");				

					if (hd.getBrowsersData((String)hd.getBrowsers(browserCounter))!=0)
					{
						output.write(Integer.toString(hd.getBrowsersData((String)hd.getBrowsers(browserCounter))));
					}	
					output.write("</Browser>");				
					output.newLine();

				}
				output.write("</BrowserData>");	
				output.newLine();
			}

			output.write("</HitsData>");	
			output.newLine();

			output.write("</openXplore>");	
			output.close();	
		}
		catch(IOException ie)
		{
		}
	}

	public void printHTML(File file, File aDir) throws TransformerException, TransformerConfigurationException, 
           FileNotFoundException, IOException
	{
		TransformerFactory tFactory = TransformerFactory.newInstance();
	
		// Use the TransformerFactory to instantiate a Transformer that will work with  
		// the stylesheet you specify. This method call also processes the stylesheet
		// into a compiled Templates object.
	
		Transformer transformer = tFactory.newTransformer(new StreamSource("batch-properties\\urlwise.xsl"));

		// Use the Transformer to apply the associated Templates object to an XML document
		// (file.xml) and write the output to a file (file.html).
		transformer.transform(new StreamSource(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file), new StreamResult(new FileOutputStream(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file +"urlwise.html")));


		Transformer transformer1 = tFactory.newTransformer(new StreamSource("batch-properties\\monthwise.xsl"));

		// Use the Transformer to apply the associated Templates object to an XML document
		// (file.xml) and write the output to a file (file.html).
		transformer1.transform(new StreamSource(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file), new StreamResult(new FileOutputStream(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file +"monthwise.html")));


		Transformer transformer2 = tFactory.newTransformer(new StreamSource("batch-properties\\yearwise.xsl"));

		// Use the Transformer to apply the associated Templates object to an XML document
		// (file.xml) and write the output to a file (file.html).
		transformer2.transform(new StreamSource(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file), new StreamResult(new FileOutputStream(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file +"yearwise.html")));
		
		Transformer transformer3 = tFactory.newTransformer(new StreamSource("batch-properties\\hourwise.xsl"));

		// Use the Transformer to apply the associated Templates object to an XML document
		// (file.xml) and write the output to a file (file.html).
		transformer3.transform(new StreamSource(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file), new StreamResult(new FileOutputStream(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file +"hourwise.html")));
		
		Transformer transformer4 = tFactory.newTransformer(new StreamSource("batch-properties\\daywise.xsl"));

		// Use the Transformer to apply the associated Templates object to an XML document
		// (file.xml) and write the output to a file (file.html).
		transformer4.transform(new StreamSource(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file), new StreamResult(new FileOutputStream(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file +"daywise.html")));

		Transformer transformer5 = tFactory.newTransformer(new StreamSource("batch-properties\\statuscode.xsl"));

		// Use the Transformer to apply the associated Templates object to an XML document
		// (file.xml) and write the output to a file (file.html).
		transformer5.transform(new StreamSource(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file), new StreamResult(new FileOutputStream(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file +"statuscode.html")));

		if (apacheextended == "Y")
		{
			Transformer transformer6 = tFactory.newTransformer(new StreamSource("batch-properties\\browser.xsl"));

			// Use the Transformer to apply the associated Templates object to an XML document
			// (file.xml) and write the output to a file (file.html).
			transformer6.transform(new StreamSource(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file), new StreamResult(new FileOutputStream(ConfigData.outputdir + openXplore.PATHSEPERATOR + aDir.getName() + openXplore.PATHSEPERATOR +file +"browser.html")));
		}


	}	
	
	public void printMainHTML(File file, File aDir)
	{
		try
		{			
			mainWriter = new FileWriter(ConfigData.outputdir + openXplore.PATHSEPERATOR  + aDir.getName() + openXplore.PATHSEPERATOR +file.getName()+ "main.html");
			mainOutput = new BufferedWriter (mainWriter);
	
			mainOutput.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
			mainOutput.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
			mainOutput.write("<head>");
			mainOutput.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
			mainOutput.write("<title>OpenXplore - Main Results File</title>");
			mainOutput.write("<style type=\"text/css\">");
			mainOutput.write("<!--");
			mainOutput.write(".style5 {font-size: 36px; font-weight: bold; }");
			mainOutput.write(".style3 {");
			mainOutput.write("font-size: 16px;");
			mainOutput.write("font-weight: bold;");
			mainOutput.write("}");
			mainOutput.write(".style2 {");
			mainOutput.write("	font-size: 18px;");
			mainOutput.write("	font-weight: bold;");
			mainOutput.write("	}");
			mainOutput.write(".style7 {font-size: 16px}");
			mainOutput.write("-->");
			mainOutput.write("</style>");
			mainOutput.write("</head>");

			mainOutput.write("<body>");
			mainOutput.write("<table width=\"64%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#000000\">");
			mainOutput.write("<table width=\"244\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
			mainOutput.write("<tr><td width=\"234\"><span class=\"style5\">OpenX</span><span class=\"style5\">plore</span></span></td></tr>");
			mainOutput.write("</table>");
  			mainOutput.write("<tr>");
			mainOutput.write("    <td><div align=\"left\">");
       		mainOutput.write("  <blockquote>");
        	      mainOutput.write("	<blockquote>");
	            mainOutput.write("  		<p><span class=\"style5\"> The Open Source Web Analytics Solution</span></p>");
    	            mainOutput.write("	</blockquote>");
      		mainOutput.write("  </blockquote>");
		      mainOutput.write("</div></td>");
			mainOutput.write("  </tr>");
			mainOutput.write("</table>");
			mainOutput.write("<p>&nbsp;</p>");
			mainOutput.write("<table width=\"491\" height=\"353\" border=\"1\">");
			mainOutput.write("<tr>");
			mainOutput.write("<td width=\"279\"><strong>Available Reports For Your Log File </strong></td>");
			mainOutput.write("<td width=\"279\"><strong>" +file.getName() +"</strong></td>");
			mainOutput.write("</tr>");
			mainOutput.write("<tr>");
		      mainOutput.write("<td><strong>URLWise Hits Summary</strong></td>");
			mainOutput.write("<td><a href=http://" +ConfigData.hostname +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/" +file.getName() +".xmlurlwise.html" +">http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() + ".xmlurlwise.html</a></td>");
			mainOutput.write("</tr>");
			mainOutput.write("<tr>");
		      mainOutput.write("<td><strong>HourWise Hits Summary</strong></td>");
			mainOutput.write("<td><a href=http://" +ConfigData.hostname +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() +".xmlhourwise.html" +">http://" +ConfigData.hostname +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() + ".xmlhourwise.html</a></td>");
			mainOutput.write("</tr>");
			mainOutput.write("<tr>");
		      mainOutput.write("<td><strong>DayWise Hits Summary</strong></td>");
			mainOutput.write("<td><a href=http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() +".xmldaywise.html" +">http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() + ".xmldaywise.html</a></td>");
			mainOutput.write("</tr>");
			mainOutput.write("<tr>");
		      mainOutput.write("<td><strong>MonthWise Hits Summary</strong></td>");
			mainOutput.write("<td><a href=http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() +".xmlmonthwise.html" +">http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() + ".xmlmonthwise.html</a></td>");
			mainOutput.write("</tr>");
			mainOutput.write("<tr>");
		      mainOutput.write("<td><strong>YearWise Hits Summary</strong></td>");
			mainOutput.write("<td><a href=http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() +".xmlyearwise.html" +">http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/" +file.getName() + ".xmlyearwise.html</a></td>");
			mainOutput.write("</tr>");
			mainOutput.write("<tr>");
		      mainOutput.write("<td><strong>StatusCodeWise Hits Summary</strong></td>");
			mainOutput.write("<td><a href=http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() +".xmlurlwise.html" +">http://" +ConfigData.hostname  +":" +ConfigData.port +"/"  +"server-openxplore/output/" + aDir.getName() +"/" +file.getName() + ".xmlurlwise.html</a></td>");
			mainOutput.write("</tr>");
			if (apacheextended == "Y")
			{
				mainOutput.write("<tr>");
		      	mainOutput.write("<td><strong>Browser Information Summary</strong></td>");
				mainOutput.write("<td><a href=http://" +ConfigData.hostname  +":" +ConfigData.port +"/" +"server-openxplore/output/" + aDir.getName() +"/"  +file.getName() +".xmlbrowser.html" +">http://" +ConfigData.hostname  +":" +ConfigData.port + "/"  +"server-openxplore/output/" + aDir.getName() +"/"   +file.getName() + ".xmlbrowser.html</a></td>");
				mainOutput.write("</tr>");
			}
			mainOutput.write("</table>");

	            mainOutput.write("<p><span class=\"style5\">=======================================================</span></p>");
	            mainOutput.write("<p><span class=\"style5\">Report Generated By: www.pecats.com</span></p>");

			mainOutput.close();
			
		}
		catch(IOException ie)
		{
			
		}

	}

}